package br.ufrj.ppgi.jemf_advanced_example.location;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Location;
import br.ufrj.ppgi.jemf.mobile.view.LocationView;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Location detail screen.
 * This fragment is either contained in a {@link LocationListActivity} in two-pane mode (on tablets) or a {@link LocationDetailActivity} on handsets.
 */
public class LocationDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_LOCATION = "activated_location";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "LOCATION";
    /**
     * The Location content this fragment is showing.
     */
    private static Location location;
    /**
     * The Location view holder.
     */
    private static LocationView locationView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public LocationDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Location content specified by the fragment arguments.
        if (getArguments().containsKey(LocationDetailFragment.ARG_ID)) {
        	location = getArguments().getParcelable(LocationDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Location content
	    if (location != null) {
	    	locationView = new LocationView(getActivity(), false);
	    	// Status
	    	locationView.setLocationStatusText(location.getStatusString());
	    	// Time Stamp
	    	locationView.setLocationTimeText(location.getTimeStampString());
	    	// End Date
	    	locationView.setLocationIntervalText(String.valueOf(location.getInterval()));

	    	// Its necessary to add the LocationView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Location View as a child of the ScrollView.
	    	scrollView.addView(locationView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Location
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_LOCATION)) {
        	location = savedInstanceState.getParcelable(STATE_LOCATION);
        }
	}





	/**
     * Get updated Location
     * @return Location
     */
	public static Location getLocation() {
		if (BuildConfig.DEBUG) Log.i(LocationListActivity.TAG, "LocationDetailFragment ::: getLocation");
    	return location;
    }

	/**
	 * Update Location data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((location != null) && (locationView != null)) {
    		if (BuildConfig.DEBUG) Log.i(LocationListActivity.TAG, "LocationDetailFragment ::: updateDataFromUI");
    		// Update Location object with new data included by user through components
    		location.setStatusString(locationView.getLocationStatusText());
    		location.setTimeStampString(locationView.getLocationTimeText());
    		location.setInterval(Integer.valueOf(locationView.getLocationIntervalText()));
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Location.
        outState.putParcelable(STATE_LOCATION, location);
    }
}
